/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cuong.bean;

import client.entity.Category;
import client.entity.Cv;
import client.entity.LevelJob;
import client.entity.Location;
import client.entity.Worker;
import client.entity.WorkerSubject;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author Admin Windows 8
 */
@ManagedBean
@SessionScoped
public class TopTenWorkerCV {
    private List<Cv> listCV;

    /**
     * Creates a new instance of TopTenWorkerCV
     */
    public TopTenWorkerCV() {
    }
    private List<WorkerSubject> list;

    public List<WorkerSubject> getList() {
        list = new ArrayList<WorkerSubject>();       
         List<Cv> listCv = getListCv();
         for (Cv cv : listCv) {
            WorkerSubject ws= new WorkerSubject();
            ws.setIdcv(cv.getId());
            ws.setTitlecv(cv.getTitle());
            if(cv.getTypeJob()==true){
                ws.setTypejob("Full Time");
            }else{
                ws.setTypejob("Pass Time");
            }
            ws.setNamewk(getWorkerByID(cv.getWorker().getId()).getName());
            ws.setNameleveljob(getLevelJobByID(cv.getLevelJob().getId()).getName());
            
            List<Category> listCate= getListCateByIdCV(cv.getId());
            StringBuffer stringCate= new StringBuffer();
            for(int i=0;i<listCate.size();i++){
                Category ct=listCate.get(i);
                if(i<listCate.size()-1){
                    stringCate.append(ct.getCategoryName()+", ");
                }else{
                    stringCate.append(ct.getCategoryName());
                }
            }
            ws.setNamecategory(stringCate.toString());
            
            List<Location> listLoca=getListLocaByIdCV(cv.getId());            
            StringBuffer stringLocale= new StringBuffer();
             for(int i=0;i<listLoca.size();i++){
                Location lc=listLoca.get(i);
                if(i<listLoca.size()-1){
                    stringLocale.append(lc.getName()+", ");
                }else{
                    stringLocale.append(lc.getName());
                }
            }
             ws.setNamelocation(stringLocale.toString());
            list.add(ws);
        }
        return list;
    }

    public void setList(List<WorkerSubject> list) {
        this.list = list;
    }
    
    public List<Cv> getListCv() {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
       listCV=ss.createQuery("from Cv cv order by cv.id desc").setMaxResults(10).list();
       ss.getTransaction().commit();
        ss.close();
        return listCV;
    }
    
     public List<Category> getListCateByIdCV(int idCV){
        List<Category> list = new ArrayList<Category>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("select c from Category as c inner join c.cvCategories as rc where rc.cv.id="+idCV).list();       
        ss.getTransaction().commit();
        ss.close();
        return list;
    }
    
    public List<Location> getListLocaByIdCV(int idCV){
        List<Location> list = new ArrayList<Location>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("select lo from Location as lo inner join lo.cvLocations as cl where cl.cv.id="+idCV).list();       
        ss.getTransaction().commit();
        ss.close();
        return list;
    }
    
     public LevelJob getLevelJobByID(int id){
        LevelJob lj = new LevelJob();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        lj = (LevelJob) ss.createQuery("from LevelJob lj where lj.id="+id).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return lj;
    }
     public Worker getWorkerByID(int id){
        Worker wk = new Worker();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        wk = (Worker) ss.createQuery("from Worker wk where wk.id="+id).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return wk;
    }
}
